//
//  Date+Extension.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/28.
//  Copyright © 2018 imguider. All rights reserved.
//

import Foundation

extension Date {
    
    /// 将日期转换成指定格式字符串
    ///
    /// - Parameters:
    ///   - format: e.g.:yyyy-mm-dd
    ///   - localIdenti: 默认语言
    /// - Returns: --
    func string(_ format:String,localIdenti:String? = nil) -> String? {
        
        let dateformatter = DateFormatter()
        
        dateformatter.dateFormat = format
        
        if let iden = localIdenti {
            
            dateformatter.locale = Locale(identifier: iden)
        }
        
        return dateformatter.string(from: self)
    }
    
    
    /// 将日期字符串 转换成日期对象
    ///
    /// - Parameters:
    ///   - string: 日期字符串
    ///   - format: 日期格式
    /// - Returns: --
    static func date(_ string:String, _ format: String) -> Date? {
        
        let dateformatter = DateFormatter()
        
        dateformatter.dateFormat = format
        
        return dateformatter.date(from: string)
    }
}
